package com.xzx.study.animation;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.xzx.study.R;

public class AnimationApp2Activity extends AppCompatActivity {

    private ImageView imageView;
    private TextView textView;
    private ProgressBar progressBar;

    @SuppressLint("StaticFieldLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_app2);
        imageView = findViewById(R.id.scan_img);
        textView = findViewById(R.id.scan_tip1);
        progressBar = findViewById(R.id.scan_progress_bar);

        RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
                                                              Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        imageView.startAnimation(rotateAnimation);

        new AsyncTask<Void, Void, Void>() {
            @Override
            protected void onPreExecute() {
                textView.setText("杀毒引擎正在扫描中.......");
            }

            @Override
            protected Void doInBackground(Void... voids) {
                int max = 60;
                progressBar.setMax(max);
                for (int i = 1; i <= max; i++) {
                    SystemClock.sleep(100);
                    publishProgress();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                textView.setText("扫描完成未发现病毒应用，请放心使用");
                progressBar.setVisibility(View.GONE);
                imageView.clearAnimation();
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                progressBar.incrementProgressBy(1);
            }
        }.execute();
    }
}
